package cn.itsource.hrm.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMQConfig {
    public static final String QUEUE_INFORM_EMAIL = "queue_name_email"; //邮件
    public static final String QUEUE_INFORM_SMS = "queue_name_sms"; // 短信
    public static final String QUEUE_INFORM_INNERMSG = "queue_name_innermsg"; // 站内信
    public static final String EXCHANGE_DIRECT_INFORM = "exchange_direct_course"; //交换机

    public static final String EMAIL_ROUTING_KEY="email";
    public static final String SMS_ROUTING_KEY="sms";
    public static final String INNERMSG_ROUTING_KEY="innermsg";

    //创建交互机
    @Bean
    public Exchange getExchange(){
        //使用订阅的模式的交互机
       return ExchangeBuilder.directExchange(EXCHANGE_DIRECT_INFORM).durable(true).build();
    }

    //创建邮件队列
    @Bean
    public Queue getEmailQueue(){
        return  new Queue(QUEUE_INFORM_EMAIL);
    }

    //创建短信队列
    @Bean
    public Queue getSMSQueue(){
        return  new Queue(QUEUE_INFORM_SMS);
    }

    //创建站内信队列
    @Bean
    public Queue getINNERQueue(){
        return  new Queue(QUEUE_INFORM_INNERMSG);
    }

    //绑定队列
    @Bean
    public Binding bindingEmail(){
        // 使用Buidler创建绑定，指定队列名和交换机的命中以及routingkey 和 参数
       return BindingBuilder.bind(getEmailQueue()).to(getExchange()).with(EMAIL_ROUTING_KEY).noargs();
    }

    //绑定队列
    @Bean
    public Binding bindingSMS(){
        // 使用Buidler创建绑定，指定队列名和交换机的命中以及routingkey 和 参数
       return BindingBuilder.bind(getSMSQueue()).to(getExchange()).with(SMS_ROUTING_KEY).noargs();
    }

    //绑定队列
    @Bean
    public Binding bindingINNER(){
        // 使用Buidler创建绑定，指定队列名和交换机的命中以及routingkey 和 参数
       return BindingBuilder.bind(getINNERQueue()).to(getExchange()).with(INNERMSG_ROUTING_KEY).noargs();
    }

    @Bean
    // 注意要导qmqp的包
    public MessageConverter getConverter(){
        return new Jackson2JsonMessageConverter();
    }


}
